<?php

namespace common\models\member;

use Yii;
use common\models\user\UserBranch;

/**
 * This is the model class for table "{{%member_branch_log}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $member_id 会员ID
 * @property int $original_member_id
 * @property int $admin_id 操作人ID
 * @property string $type
 * @property string $mark 备注
 * @property int $status 操作状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at
 */
class MemberBranchLog extends \common\db\ActiveRecord {

    const TYPE_BIND = 'bind';
    const TYPE_RELEASE = 'release';
    const TYPE_REMOVE = 'remove';
    const TYPE_GROWUP = 'growup';
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_branch_log}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'member_id', 'original_member_id', 'admin_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['type'], 'string', 'max' => 20],
            [['mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'member_id' => '会员ID',
            'original_member_id' => 'Original Member ID',
            'admin_id' => '操作人ID',
            'type' => 'Type',
            'mark' => '备注',
            'status' => '操作状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function getTypeMark($type) {
        if ($type == static::TYPE_BIND) {
            return '代理新关注自动绑定';
        } elseif ($type == static::TYPE_RELEASE) {
            return '解除释放关注关系';
        } elseif ($type == static::TYPE_REMOVE) {
            return '代理资格取消，移除关注关系';
        } elseif ($type == static::TYPE_GROWUP) {
            return '成长变更，成为代理人';
        }
        return 'Unkown';
    }

    /**
     * 设置关注绑定日志
     * @param type $type
     * @param UserBranch $branch
     * @param type $mark
     */
    public function setTypeLog($type, $branch, $mark = '') {
        $this->user_id = $branch->user_id;
        $this->member_id = $branch->member_id;
        $this->type = $type;
        if (!$mark) {
            $mark = $this->getTypeMark($type);
        }
        $this->mark = $mark;
        $this->status = static::STATUS_ACTIVE;
    }

}
